package com.xuerhua.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.internal.$Gson$Preconditions;
import com.xuerhua.yxt.model.OrganizeGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.TeacherLeave;
import com.xuerhua.yxt.model.TeacherOrganizeGroup;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.TeacherLeaveReqVO;
import com.xuerhua.yxt.service.IOrganizeGroupService;
import com.xuerhua.yxt.service.ITeacherLeaveService;
import com.xuerhua.yxt.service.IWorkApprovalService;
import com.xuerhua.yxt.utils.DateUtil;
import com.xuerhua.yxt.view.TeacherLeaveView;
import com.xuerhua.yxt.vo.TeacherLeaveQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.TEACHER_LEAVE)
@Controller
public class TeacherLeaveController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(TeacherLeaveController.class);
	@Autowired
	private IWorkApprovalService workApprovalService;
	@Autowired
	private ITeacherLeaveService teacherLeaveService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	
	/**
	 * 老师请假页面
	 * @return
	 */
	@RequestMapping(value = {"","/"})
	public String teacherLeave(){
		AppUser appUser = getAppUser();
		if(workApprovalService.isApprover(appUser.getTeacher().getId())) {
			return "teacherLeave/approverList";
		}
		return "teacherLeave/list";
	}
	
	@RequestMapping("publish")
	public String publish(Model model) {
		model.addAttribute("relationType", RelationType.TEACHER_LEAVE.getCode());
		return "teacherLeave/publish";
	}
	
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(TeacherLeaveQueryVO vo) {
		try {
			Page<TeacherLeaveView> page = new Page<TeacherLeaveView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			sorts.add(new OrderBy("UPDATE_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<TeacherLeaveView> list = teacherLeaveService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	/**
	 * 老师请假申请
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("create")
	@ResponseBody Object teacherLeaveCreate(TeacherLeaveReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
			reqVO.setTermId(getCurrentTerm().getId());
			reqVO.setTeacherId(appUser.getTeacher().getId());
			reqVO.setTeacherName(appUser.getTeacher().getName());
			if(reqVO.getIds() != null) {
				reqVO.setIsAttach(1);
			}
			reqVO.setStartTime(DateUtil.strToDate(reqVO.getStartDate(), DateUtil.DEFAULT_DATE_FORMAT1));
			reqVO.setEndTime(DateUtil.strToDate(reqVO.getEndDate(), DateUtil.DEFAULT_DATE_FORMAT1));
			teacherLeaveService.insert(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---studentLeaveCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("audit")
	@ResponseBody Object teacherStudentLeaveAudit(TeacherLeave record) {
		try {
			record.setSchoolId(getAppUser().getTeacher().getSchoolId());
			int result = teacherLeaveService.updateAudit(record);
			if(result > 0) {
				return new RspResult(Status.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("---teacherStudentLeaveAudit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("approver")
	@ResponseBody Object getApprover(String approvalType) {
		try {
			AppUser appUser = getAppUser();
			//组织级别：行政组，级组长
			String groupIds = "10,20";
			OrganizeGroup og = new OrganizeGroup();
			og.setIds(groupIds);
			og.setSchoolId(appUser.getTeacher().getSchoolId());
			List<TeacherOrganizeGroup> list = organizeGroupService.fillTeacherOrganizeGroupByGroupIds(og);
			return new RspResult(Status.SUCCESS, list);
		} catch (Exception e) {
			logger.error("---getApprover error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
